﻿/*
 * blood_pressure表的数据操作
 * */
using System;
using System.Collections.Generic;
using System.Text;
using WH.NurseSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace WH.NurseSys.DAL
{
    public class BloodPressureDAL
    {        
        public bool Insert(BloodPressure bloodPressure)
        {
            string sqlText = "insert into blood_pressure (id,patient_id,sys,dia,pulse,exec_time) values (@id,@patient_id,@sys,@dia,@pulse,@exec_time)";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@id",bloodPressure.Id),
                new SqlParameter("@patient_id",bloodPressure.PatientId),
                new SqlParameter("@sys",bloodPressure.Sys),
                new SqlParameter("@dia",bloodPressure.Dia),
                new SqlParameter("@pulse",bloodPressure.Pulse),
                new SqlParameter("@exec_time",bloodPressure.ExecTime)
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        public bool Delete(Guid id)
        {
            string sqlText = "delete from blood_pressure where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        public bool Update(BloodPressure bloodPressure)
        {
            string sqlText = "update blood_pressure set patient_id=@patient_id,sys=@sys,dia=@dia,pulse=@pulse,exec_time=@exec_time where id=@id";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@id",bloodPressure.Id),
                new SqlParameter("@patient_id",bloodPressure.PatientId),
                new SqlParameter("@sys",bloodPressure.Sys),
                new SqlParameter("@dia",bloodPressure.Dia),
                new SqlParameter("@pulse",bloodPressure.Pulse),
                new SqlParameter("@exec_time",bloodPressure.ExecTime)
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        public BloodPressure GetById(Guid id)
        {
            string sqlText = "select * from blood_pressure where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            if (dbTable.Rows.Count <= 0)
                return null;
            DataRow dbRow = dbTable.Rows[0];
            BloodPressure bloodPressure = new BloodPressure();
            bloodPressure.Id = new Guid(dbRow["id"].ToString());
            bloodPressure.PatientId = new Guid(dbRow["patient_id"].ToString());
            bloodPressure.Sys = Convert.ToInt32(dbRow["sys"]);
            bloodPressure.Dia = Convert.ToInt32(dbRow["dia"]);
            bloodPressure.Pulse = Convert.ToInt32(dbRow["pulse"]);
            bloodPressure.ExecTime = Convert.ToDateTime(dbRow["exec_time"]);
            return bloodPressure;
        }

        //按人获取列表
        public List<BloodPressure> GetListByPatient(Guid patientId)
        {
            List<BloodPressure> bloodPressureList = new List<BloodPressure>();
            string sqlText = "select * from blood_pressure where patient_id=@patient_id order by exec_time asc";
            SqlParameter parameters = new SqlParameter("@patient_id", patientId);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            foreach (DataRow dbRow in dbTable.Rows)
            {
                BloodPressure bloodPressure = new BloodPressure();
                bloodPressure.Id = (Guid)dbRow["id"];
                bloodPressure.Dia = (int)dbRow["dia"];
                bloodPressure.ExecTime = Convert.ToDateTime(dbRow["exec_time"]);
                bloodPressure.PatientId = (Guid)dbRow["patient_id"];
                bloodPressure.Pulse = (int)dbRow["pulse"];
                bloodPressure.Sys = (int)dbRow["sys"];
                bloodPressureList.Add(bloodPressure);
            }
            return bloodPressureList;
        }
    }
}
